/*
 * 

 * 
 */
package com.cheche.dn.common;

import com.cheche.dn.common.Order.Direction;
import com.google.common.collect.Lists;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import java.io.Serializable;
import java.util.List;

/**
 * 分页信息
 * 
 * 
 * @version 1.0
 */
public class Pageable implements Serializable {

	private static final long serialVersionUID = -3930180379790344299L;

	/** 默认页码 */
	private static final int DEFAULT_PAGE_NUMBER = 1;

	/** 默认每页记录数 */
	public static final int DEFAULT_PAGE_SIZE = 20;

	/** 最大每页记录数 */
	private static final int MAX_PAGE_SIZE = 1000;

	/** 页码 */
	private int pageNumber = DEFAULT_PAGE_NUMBER;

	/** 每页记录数 */
	private int pageSize = DEFAULT_PAGE_SIZE;

	/** 筛选 */
	private List<Filter> filters = Lists.newArrayList();

	/** 排序 */
	private List<Order> orders = Lists.newArrayList();

	/**
	 * 初始化一个新创建的Pageable对象
	 */
	public Pageable() {
	}

	/**
	 * 初始化一个新创建的Pageable对象
	 * 
	 * @param pageNumber
	 *            页码
	 * @param pageSize
	 *            每页记录数
	 */
	public Pageable(Integer pageNumber, Integer pageSize) {
		if (pageNumber != null && pageNumber >= 1) {
			this.pageNumber = pageNumber;
		}
		if (pageSize != null && pageSize >= 1 && pageSize <= MAX_PAGE_SIZE) {
			this.pageSize = pageSize;
		}
	}

	/**
	 * 获取页码
	 * 
	 * @return 页码
	 */
	public int getPageNumber() {
		return pageNumber;
	}

	/**
	 * 设置页码
	 * 
	 * @param pageNumber
	 *            页码
	 */
	public void setPageNumber(int pageNumber) {
		if (pageNumber < 1) {
			pageNumber = DEFAULT_PAGE_NUMBER;
		}
		this.pageNumber = pageNumber;
	}

	/**
	 * 获取每页记录数
	 * 
	 * @return 每页记录数
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置每页记录数
	 * 
	 * @param pageSize
	 *            每页记录数
	 */
	public void setPageSize(int pageSize) {
		if (pageSize < 1 || pageSize > MAX_PAGE_SIZE) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		this.pageSize = pageSize;
	}

	/**
	 * 添加排序属性 默认asc排序
	 *
	 * @param orderProperty 排序属性
	 *
	 * @return Pageable
	 */
	public Pageable addOrder(String orderProperty) {
		this.orders.add(new Order(orderProperty, Direction.asc));
		return this;
	}

	/**
	 * 添加排序属性
	 * 
	 * @param orderProperty 排序属性
     *
     * @param orderDirection 排序方向
	 *
     * @return Pageable
	 */
	public Pageable addOrder(String orderProperty, Direction orderDirection) {
		this.orders.add(new Order(orderProperty, orderDirection));
		return this;
	}

    /**
     * 添加排序属性
     *
     * @param orders 排序属性列表
     *
     * @return Pageable
     */
    public Pageable addOrders(List<Order> orders) {
        this.orders.addAll(orders);
        return this;
    }

	/**
	 * 设置排序属性,会覆盖排序属性
	 *
	 * @param orders 排序属性列表
	 *
	 * @return Pageable
	 */
	public Pageable setOrders(List<Order> orders) {
		this.orders = orders;
		return this;
	}

    /**
     * 获取排序
     *
     * @return 排序
     */
    public List<Order> getOrders() {
        return orders;
    }

	/**
	 * 获取筛选
	 * 
	 * @return 筛选
	 */
	public List<Filter> getFilters() {
		return filters;
	}

	/**
	 * 添加筛选
	 * 
	 * @param filter
	 *            筛选
     * @return Pageable
	 */
    public Pageable addFilter(Filter filter) {
        this.filters.add(filter);
        return this;
    }

    /**
     * 添加筛选
     *
     * @param filters 筛选列表
     *
     * @return Pageable
     */
    public Pageable addFilters(List<Filter> filters) {
        this.filters.addAll(filters);
        return this;
    }

	/**
	 * 设置筛选,会覆盖筛选
	 *
	 * @param filters 筛选列表
	 *
	 * @return Pageable
	 */
	public Pageable setFilters(List<Filter> filters) {
		this.filters = filters;
		return this;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		Pageable other = (Pageable) obj;
		return new EqualsBuilder().append(getPageNumber(), other.getPageNumber()).append(getPageSize(),
                other.getPageSize()).append(getFilters(), other.getFilters()).append(getOrders(), other.getOrders()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(getPageNumber()).append(getPageSize()).append(getFilters()).append(getOrders()).toHashCode();
	}

}